﻿using BacklogParser.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace BacklogParser.Parsers
{
    public static class PublicationReferenceParser
    {
        public static PublicationReference Parse(XElement claims)
        {
            var priorityClaims = claims.Descendants(Constants.Epo + "document-id").ToList();
            return ParsePublicationReference(priorityClaims);
        }

        private static PublicationReference ParsePublicationReference(List<XElement> priorityClaims)
        {
            PublicationReference result = new PublicationReference();

            var documentIds = new List<DocumentId>();

            foreach (var item in priorityClaims)
            {
                var document = new DocumentId
                {
                    Country = item.Descendants(Constants.Epo + "country").First().Value,
                    Date = item.Descendants(Constants.Epo + "date").First().Value,
                    DocNumber = item.Descendants(Constants.Epo + "doc-number").First().Value,
                    Kind = item.Descendants(Constants.Epo + "kind").First().Value,
                };

                documentIds.Add(document);
            }

            result.DocumentIds = documentIds;

            return result;
        }
    }
}
